<!--  -->
<template>
  <div class="vod">
    <div class="meedu-filter-box">
      <div class="filter-box">
        <div class="filter-back">
          <img src="../assets/img/icon-back.png" class="back" @click="goBack" />
        </div>
        <div class="filter-title">录播课程</div>
        <div class="filter-button" @click="showFilterList">
          <div class="button-text">
            <span>筛选</span><img src="../assets/img/icon-filter.png" />
          </div>
          <div v-if="isShow" class="category2-box">
            <div class="item" @click="changeTab(0)" :class="{ active: current == 0 }">全部</div>
            <div
              class="item"
              v-for="item in fillterList"
              :key="item.id"
              :class="{ active: current == item.id }"
              @click="changeTab(item.id)"
            >
              {{ item.name }}
            </div>
          </div>
          <div v-if="isShow" class="meedu-shadow-box"></div>
        </div>
      </div>
      <div class="category1-box">
        <div
          class="item"
          v-for="(item, index) in items"
          :key="item"
          :class="{ current: nowIndex == index }"
          @click="changeIndex(index)"
        >
          {{ item }}
        </div>
      </div>
    </div>
    <div class="gray"></div>
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      offset="5"
      @load="onLoad"
    >
      <div
        class="vod-course-item"
        v-for="item in courseLista"
        :key="item.id"
        @click="goToClassDetail(item)"
      >
        <div class="vod-course-item-comp" :videos-coun="item.videos_count">
          <div class="vod-course-thumb">
            <img :src="item.thumb" />
          </div>
          <div class="vod-course-body">
            <div class="vod-course-title">{{ item.title }}</div>
            <div class="vod-course-info">
              <div class="vod-course-sub">{{ item.user_count }}人已订阅</div>
              <div class="vod-course-charge">
                <div v-if="item.is_free == 0" class="charge-text">
                  <span class="unit">￥</span>{{ item.charge }}
                </div>
                <div v-else class="free-text">免费</div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </van-list>
    <div class="drop">
      <img src="../assets/img/Spinload.gif" style="display: none" />
    </div>
  </div>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';
import Vue from "vue";
import { List } from "vant";

Vue.use(List);
export default {
  //import引入的组件需要注入到对象中才能使用
  components: {},
  data() {
    //这里存放数据
    return {
      courseList: [],
      fillterList: [],
      allCourseList:[],
      items: ["全部", "免费"],
      isShow: false,
      current: 0,
      nowIndex: 0,
      showedCategoryId: 0,
      loading: false,
      finished: false,
      page: 0,
      size: 10,
      total: 0,
    };
  },
  //监听属性 类似于data概念
  computed: {
    courseLista: function () {
      let showId = this.showedCategoryId;
      let isFree = this.nowIndex;
      return this.courseList.filter(function (item) {
        if (isFree == 0) {
          return showId == 0 || showId == item.category_id;
        } else {
          return item.is_free == 1 && (showId == 0 || showId == item.category_id);
        }
      });
    },
  },
  //监控data中的数据变化
  watch: {},
  //方法集合
  methods: {
    showFilterList() {
      this.isShow = !this.isShow;
    },
    changeTab(index) {
      this.current = index;
      this.showedCategoryId = index;
    },
    changeIndex(index) {
      this.nowIndex = index;
    },
    goBack() {
      this.$router.go(-1);
    },
    goToClassDetail(item) {
      this.$router.push({
        path: "/vod/detail",
        query: {
          id: item.id,
        },
      });
    },
    async onLoad() {
      // this.loading = false;
      this.page = this.page + 1;
      let res = await this.$request.get("api/v2/courses", {
        params: {
          page: this.page,
          page_size: this.size,
          category_id: 0,
          scene: "",
        },
      });
      this.total = res.data.total;
      this.courseList = [...this.courseList, ...res.data.data];
      this.loading = false;
      if (this.courseList.length >= res.data.total) {
        this.finished = true;
      }
    },
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  async created() {
    let res = await this.$request.get("api/v2/courses");
    this.allCourseList = res.data.data;
    let res1 = await this.$request.get("api/v2/course_categories");
    this.fillterList = res1.data;
  },
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {},
  beforeCreate() {}, //生命周期 - 创建之前
  beforeMount() {}, //生命周期 - 挂载之前
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {}, //如果页面有keep-alive缓存功能，这个函数会触发
};
</script>
<style lang="less" scoped>
.current {
  color: #3ca7fa !important;
  font-weight: 700;
}
.vod {
  position: absolute;
  left: 0;
  top: 0;
  overflow-y: scroll;
  overflow-x: hidden;
  width: 100%;
  height: 100%;
  .meedu-filter-box {
    width: 100%;
    height: auto;
    float: left;
    box-sizing: border-box;
    padding: 0 15px;
    background-color: #fff;
    line-height: 30px;
    position: sticky;
    top: 0;
    z-index: 999;
    display: flex;
    flex-direction: column;
    .filter-box {
      width: 100%;
      height: 50px;
      background: #fff;
      display: flex;
      flex-direction: row;
      justify-content: space-between;
      align-items: center;
      .filter-back {
        width: 24px;
        height: 24px;
        cursor: pointer;
        img.back {
          width: 24px;
          height: 24px;
        }
      }

      .filter-title {
        width: 72%;
        height: 18px;
        font-size: 18px;
        font-weight: 500;
        color: #171923;
        line-height: 18px;
        text-align: center;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }

      .filter-button {
        width: auto;
        height: auto;
        position: relative;
        .button-text {
          font-size: 14px;
          font-weight: 400;
          color: #333;
          cursor: pointer;
          display: flex;
          align-items: center;
          span {
            font-size: 14px;
            font-weight: 400;
            color: #333;
            cursor: pointer;
          }

          img {
            width: 24px;
            height: 24px;
            margin-left: 5px;
          }
        }
        .category2-box {
          position: absolute;
          top: 30px;
          right: 0;
          z-index: 10080;
          width: 166px;
          height: auto;
          box-sizing: border-box;
          padding: 0 15px;
          border-radius: 5px;
          background-color: #fff;
          .item.active {
            color: #3ca7fa !important;
            font-weight: 700;
            width: 100%;
            height: auto;
            float: left;
            font-size: 14px;
            line-height: 40px;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
          }

          .item {
            width: 100%;
            height: auto;
            float: left;
            font-size: 14px;
            color: #333;
            line-height: 40px;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
          }
        }

        .meedu-shadow-box {
          position: fixed;
          top: 0;
          left: 0;
          right: 0;
          bottom: 0;
          z-index: 997;
          background-color: rgba(0, 0, 0, 0.3);
        }
      }
    }

    .category1-box {
      width: 100%;
      height: 50px;
      background: #fff;
      display: flex;
      align-items: center;
      .item.active {
        width: auto;
        height: auto;
        float: left;
        font-size: 16px;
        color: #666;
        margin-right: 16px;
      }

      .item {
        width: auto;
        height: auto;
        float: left;
        font-size: 16px;
        color: #666;
        margin-right: 16px;
      }
    }
  }

  .gray {
    float: left;
    width: 100%;
    height: 10px;
    background: #f3f6f9;
  }
  .vod-course-item {
    width: 100%;
    height: auto;
    float: left;
    box-sizing: border-box;
    padding: 15px;
    .vod-course-item-comp {
      width: 100%;
      height: auto;
      float: left;
      display: flex;
      .vod-course-thumb {
        width: 120px;
        height: 90px;
        margin-right: 10px;
        img {
          width: 120px;
          height: 90px;
          border-radius: 4px;
        }
      }

      .vod-course-body {
        flex: 1;
        padding-top: 5px;
        .vod-course-title {
          width: 100%;
          height: 40px;
          float: left;
          font-size: 15px;
          font-weight: 500;
          color: #171923;
          line-height: 20px;
          text-overflow: -o-ellipsis-lastline;
          overflow: hidden;
          text-overflow: ellipsis;
          display: -webkit-box;
          -webkit-line-clamp: 2;
          line-clamp: 2;
          -webkit-box-orient: vertical;
          margin-bottom: 15px;
        }

        .vod-course-info {
          width: 100%;
          height: auto;
          float: left;
          display: flex;
          align-items: center;
          .vod-course-sub {
            flex: 1;
            text-align: left;
            font-size: 12px;
            font-weight: 400;
            color: #999;
            line-height: 12px;
          }

          .vod-course-charge {
            flex: 1;
            text-align: right;
            font-size: 18px;
            line-height: 18px;
            font-weight: 600;
            .charge-text {
              color: #ff5858;
              span.unit {
                font-size: 14px;
              }
            }
            .free-text {
              font-size: 14px;
              font-weight: 600;
              color: #52c41a;
            }
          }
        }
      }
    }
  }
  .drop {
    display: inline-block;
    width: 100%;
    text-align: center;
    color: #ccc;
    font-size: 14px;
    margin-top: 30px;
    margin-bottom: 30px;
    img {
      text-align: center;
      color: #ccc;
      font-size: 14px;
    }
  }
}
</style>
